import { getSongDetail } from "@/netWork/player"
import { getRandomNumber } from "@/tools/math"
import { getLyric } from "@/netWork/player"
import * as actionTypes from "./constants"
import { parseLyric } from "@/tools/parse-lyric"

//获取歌曲详情
const changeCurrentSongAction = (song) => {
    return {
        type: actionTypes.CHANGE_CURRENT_SONG,
        currentSong: song
    }
}
export const getSongDetailAction = (ids) => {
    return (dispatch, getState) => {
        //1.根据id查找playList里是否已经有了该歌曲
        const playList = getState().getIn(["player", "playList"])
        const songIndex = playList.findIndex(item => {
            return item.id === ids
        })
        //   2.判断是否在播放列表里找到了歌曲
        let song = null
        if (songIndex !== -1) {
            //找到了歌曲
            dispatch(changeCurrentSongIndexAction(songIndex))
            song = playList[songIndex]
            dispatch(changeCurrentSongAction(song))
        } else {
            //未找到歌曲
            getSongDetail(ids).then(res => {
                song = res.songs && res.songs[0]
                if (!song) return
                //把歌曲保存到播放列表里
                const newPlayLiat = [...playList]
                console.log(newPlayLiat);
                newPlayLiat.push(song)
                //更新redux里playList里的值
                dispatch(changePlayListAction(newPlayLiat))
                //更新当前歌曲下标
                dispatch(changeCurrentSongIndexAction(newPlayLiat.length - 1))
                dispatch(changeCurrentSongAction(song))
                //请求歌词
                if (!song) return
                dispatch(getLyricAction(song.id))
            })
        }
    }
}
//获取歌曲列表
const changePlayListAction = (playList) => {
    return {
        type: actionTypes.CHANGE_PLAY_LIST,
        playList
    }
}

//获取当前播放歌曲的下标
const changeCurrentSongIndexAction = (currentSongIndex) => {
    return {
        type: actionTypes.CHANGE_CURRENT_SONG_INDEX,
        currentSongIndex
    }
}
//播放顺序
export const changeOrderAction = (order) => {
    return {
        type: actionTypes.CHNAGE_ORDER,
        order
    }
}
//上一首(-1) or 下一首(1)
export const changeCurrentSong = (tag) => {
    return (dispatch, getState) => {
        const order = getState().getIn(["player", "order"])
        const playList = getState().getIn(["player", "playList"])
        let currentSongIndex = getState().getIn(["player", "currentSongIndex"])
        switch (order) {
            case 1:
                let randomIndex = getRandomNumber(playList.length)  //随机播放
                //当型循环 当随机出来的数跟正在播放音乐的下标相同时重新随机直到不相同结束循环
                if (playList.length > 1) {
                    while (randomIndex === currentSongIndex) {
                        randomIndex = getRandomNumber(playList.length)
                    }
                } else {
                    randomIndex = 0
                }
                currentSongIndex = randomIndex
                dispatch(changeCurrentSongIndexAction(currentSongIndex))
                break;
            default:
                currentSongIndex += tag //顺序播放 and 单曲循环
                if (currentSongIndex >= playList.length) {
                    //当等于音乐列表长度时切换到第一首歌
                    currentSongIndex = 0
                }
                if (currentSongIndex < 0) {
                    //当小于0时切换到最后一首歌
                    currentSongIndex = playList.length - 1
                }
                dispatch(changeCurrentSongIndexAction(currentSongIndex))
        }
        const currentSong = playList[currentSongIndex]
        dispatch(changeCurrentSongAction(currentSong))
        dispatch(getLyricAction(currentSong.id))
    }
}
//请求歌词信息
const chnageLyricListAction = (lyricList)=>{
    return {
        type:actionTypes.CHANGE_LYRIC_LIST,
        lyricList
    }
}
export const getLyricAction = (id) => {
    return dispatch => {
        getLyric(id).then(res => {
           if(res.lrc){
            const lyricList = parseLyric(res.lrc.lyric)
            dispatch(chnageLyricListAction(lyricList))
           }
        })
    }
}
//正在播放的歌词

export const changeLyricingAction = (lyricing) => {
    return {
        type:actionTypes.CHANGE_LYRICING,
        lyricing
  }
}